//
//  UITextField+Category.m
//  OralRobot
//
//  Created by 王桂敏 on 16/5/5.
//  Copyright © 2016年 wangguimin. All rights reserved.
//

#import "UITextField+Category.h"



@implementation UITextField (Category)


+ (WGMTextField *)textFieldWithFrame:(CGRect)frame
                              image:(NSString *)imageName
                        placeholder:(NSString *)placeholder
                       keyboardType:(UIKeyboardType)keyboardType
                    clearButtonMode:(UITextFieldViewMode)clearButtonMode
                           delegate:(id<UITextFieldDelegate>)delegate
{
    WGMTextField *textField = [[WGMTextField alloc] initWithFrame:frame];
    
//    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
//    [toolBar setBarStyle:UIBarStyleDefault];
//    
//    UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
//    
//    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//    btn.frame = CGRectMake(2, 5, 50, 25);
//    [btn addTarget:self action:@selector(dismissKeyBoard) forControlEvents:UIControlEventTouchUpInside];
//    [btn setImage:[UIImage imageNamed:@"btn_yincangjp_"] forState:UIControlStateNormal];
//    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc]initWithCustomView:btn];
//    NSArray * buttonsArray = [NSArray arrayWithObjects:btnSpace,doneBtn,nil];
//    [toolBar setItems:buttonsArray];
//    [textField setInputAccessoryView:toolBar];

    //  设置输入框的基本设置
    textField.backgroundColor = [UIColor whiteColor];
    textField.layer.masksToBounds = YES;
    textField.layer.cornerRadius = 5;
    textField.keyboardType = keyboardType;
    textField.clearButtonMode = clearButtonMode;
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.backgroundColor = [UIColor clearColor];
    textField.textAlignment = NSTextAlignmentLeft;
    
    textField.delegate = delegate;
    
    textField.placeholder = placeholder;
    
    //  设置输入框的占位图片
    if (imageName) {
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageName"]];
        imageView.frame = CGRectMake(0, 0,kWidth(37), CGRectGetHeight(textField.frame));
        textField.leftView = imageView;
        textField.leftViewMode = UITextFieldViewModeAlways;
        
    }

    
    return textField;
}
- (void)dismissKeyBoard{
    NSLog(@"1");
}
+ (BOOL)phoneWithBlank:(UITextField *)textField
                 range:(NSRange)range
     replacementString:(NSString *)string{
    if ([string isPhone] != PhoneTypeNo) {
        NSString *str = [NSString stringWithFormat:@"%@ %@ %@",[string substringWithRange:NSMakeRange(0, 3)],[string substringWithRange:NSMakeRange(3, 4)],[string substringWithRange:NSMakeRange(7, 4)]];
        textField.text = str;
        return NO;
    }
    NSUInteger newLength = textField.text.length - range.length + string.length;
    //添加
    if (string.length>0) {
        NSMutableString *str=[NSMutableString stringWithString:textField.text];
        
        while (textField.text.length == 3||textField.text.length == 8) {
            if (textField.text.length == 3) {
                [str insertString:@" " atIndex:3];
            }else if(textField.text.length == 8){
                [str insertString:@" " atIndex:8];
            }
            textField.text = [NSString stringWithString:str];
        }
        
    }
    //删除
    else if(string.length == 0){
        
        NSMutableString *str=[NSMutableString stringWithString:textField.text];
        while (textField.text.length == 5||textField.text.length == 10) {
            if (textField.text.length == 5) {
                [str deleteCharactersInRange:NSMakeRange(4, 1)];
            }
            else if(textField.text.length == 10){
                [str deleteCharactersInRange:NSMakeRange(9, 1)];
            }
            textField.text = [NSString stringWithString:str];
        }
    }
    // 限制输入长度为13.（分隔线也计算在内）
    if (newLength > 13)
        return NO;//限制长度
    return YES;
    
}
@end
